package ncme.auto.framework.core.callback;

import ncme.auto.framework.core.entities.ResMsg;
import ncme.auto.framework.core.entities.aspect.parameter.ServiceInfo;
import ncme.auto.framework.core.enums.Order;

public class AutoProcessControllerPart {

    private AutoProcessInterface getPartBefore = () -> null;

    private AutoProcessInterface getPartAfter = () -> null;

    private AutoProcessInterface addPartBefore = () -> null;

    private AutoProcessInterface addPartAfter = () -> null;

    private AutoProcessInterface putPartBefore = () -> null;

    private AutoProcessInterface putPartAfter = () -> null;

    private AutoProcessInterface delPartBefore = () -> null;

    private AutoProcessInterface delPartAfter = () -> null;

    public void setBeforeGetPartCallback(AutoProcessInterface autoProcess) {
        this.getPartBefore = autoProcess;
    }

    public void setAfterGetPartCallback(AutoProcessInterface autoProcess) {
        this.getPartAfter = autoProcess;
    }

    public void setBeforeAddPartCallback(AutoProcessInterface autoProcess) {
        this.addPartBefore = autoProcess;
    }

    public void setAfterAddPartCallback(AutoProcessInterface autoProcess) {
        this.addPartAfter = autoProcess;
    }

    public void setBeforePutPartCallback(AutoProcessInterface autoProcess) {
        this.putPartBefore = autoProcess;
    }

    public void setAfterPutPartCallback(AutoProcessInterface autoProcess) {
        this.putPartAfter = autoProcess;
    }

    public void setBeforeDelPartCallback(AutoProcessInterface autoProcess) {
        this.delPartBefore = autoProcess;
    }

    public void setAfterDelPartCallback(AutoProcessInterface autoProcess) {
        this.delPartAfter = autoProcess;
    }

    public ResMsg process(ServiceInfo serviceInfo) throws Throwable {
        Order presentOrder = serviceInfo.getPresentOrder();
        ResMsg result;
        String requestType = serviceInfo.getRequest().getMethod();

        switch (requestType) {
            case "GET":
                if (presentOrder.equals(Order.Before)) {
                    result = this.getPartBefore.onCallBack();
                } else {
                    result = this.getPartAfter.onCallBack();
                }
                break;
            case "POST":
                if (presentOrder.equals(Order.Before)) {
                    result = this.addPartBefore.onCallBack();
                } else {
                    result = this.addPartAfter.onCallBack();
                }
                break;
            case "PUT":
                if (presentOrder.equals(Order.Before)) {
                    result = this.putPartBefore.onCallBack();
                } else {
                    result = this.putPartAfter.onCallBack();
                }
                break;
            case "DELETE":
                if (presentOrder.equals(Order.Before)) {
                    result = this.delPartBefore.onCallBack();
                } else {
                    result = this.delPartAfter.onCallBack();
                }
                break;
            default:
                return null;
        }
        return result;
    }

}
